package com.sdry.web.controller.y;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.sdry.model.lz.Car;
import com.sdry.model.lz.Materiel;
import com.sdry.model.lz.Warehouse;
import com.sdry.model.y.Artificial;
import com.sdry.model.y.Transport;
import com.sdry.model.zc.ZcSysUserEntity;
import com.sdry.service.y.ArtificialService;
import com.sdry.utils.ExcelUtils;
import com.sdry.utils.Page;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/artificial")
public class ArtificialController {
	public static final Integer AFFECT_ROW = 0;

    @Resource
    ArtificialService artificialService;
  //跳转路径
  	@RequestMapping("/artificial")
  	public String transport(Model model){
  		
  		return "/y/artificial";
  	}
  	 public List<String> namess() {
         List<String> list = new ArrayList<>();
         list.add("序号");//0
         list.add("仓库");//1
         list.add("姓名");//2
         list.add("工号");//3
         list.add("岗位");//4
         list.add("底薪");//5
         list.add("绩效");//5
         list.add("住房补贴");//6
         list.add("交通补贴");//7
         list.add("技能补贴");//8
         list.add("通讯补贴");//9
         list.add("油补");//10
 		 list.add("工龄奖");//11
 		 list.add("高温补贴");//12
 		 list.add("其他福利");//113
 		 list.add("餐补");//14
 		 list.add("加班费"); //15
 		 list.add("个税");//16
 		 list.add("扣款");//17
 		 list.add("服务费");//18
 		 list.add("社保公司");// 19
 		 list.add("合计");//20
 		 return list;
     }
  	 @RequestMapping("/uplodeFile")
     public void importor(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request, HttpServletResponse response,HttpSession session){
     	ZcSysUserEntity staf = (ZcSysUserEntity) session.getAttribute("user");
     
     	Date now = new Date();
 		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         String savePath = request.getSession().getServletContext().getRealPath("/WEB-INF/"+file.getOriginalFilename());
         File newFile=new File(savePath);
         //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
         try {
             file.transferTo(newFile);
         } catch (IOException e) {
             e.printStackTrace();
         }
         //取文件后缀
         String subfix = savePath.lastIndexOf(".")==-1? "" : savePath.substring(savePath.lastIndexOf(".")+1);
         System.out.println(subfix);
         List<List<String>> lists = new ArrayList<>();
         //获取导入行数
         List<String> names = namess();
         int num = names.size();
         if(subfix.equals("xls")){
             try {
                 lists = ExcelUtils.readXls(savePath, request, num);
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }else if(subfix.equals("xlsx")){
             try {
                 lists = ExcelUtils.readXlsx(savePath, request,num);
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }else{
             try {
                 response.getWriter().print("0");
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }

         int flag = 1;
         if (lists != null&&lists.size()>0) {
             for (int i = 0; i < lists.size(); i++) {
                 List<String> list = new ArrayList<>();
                 list= lists.get(i);
                 Artificial  artificial2=new  Artificial();
                 String uname=list.get(2);
                 String jnum=list.get(3);
                 List<Artificial> artificials =artificialService.queryArtificalByUid(uname,jnum);
                 if(artificials.size()>0) {
                 	
                	 for (Artificial artificial : artificials) {
                		 artificial.setRemark(list.get(0));
                		 artificial.setCtime(dateFormat.format(now));
                         if (null != list.get(1)) {
                             Long id = artificialService.queryWareByName(list.get(1).trim());
                             artificial.setWarehouseid(id);
                         } else {
                       	  artificial.setWarehouseid(null);
                         }
                         if(list.get(2)!=null||!list.get(2).equals("")||list.get(3)!=null||!list.get(3).equals("")) {
                        	
                        	 Long id=artificialService.queryUserByNameAndNum(uname,jnum);
                       	  artificial.setUid(id);
                       	artificial.setJobnum(jnum);
                         }else{
                       	  artificial.setUid(null);
                       	 
                         }
                         if (null != list.get(4)) {
                        	 Long id = artificialService.queryPostByName(list.get(4).trim());
                             artificial.setPid(id);
                             
                         } else {
                       	  artificial.setPid(null);
                         }
                         if (null != list.get(5)) {
                              artificial.setBasesalary(new BigDecimal(list.get(5)));
                          } else {
                       	  artificial.setBasesalary(new BigDecimal(0));
                         }
                         if (null != list.get(6)) {
                             artificial.setAchievement(new BigDecimal(list.get(6)));
                         } else {
                      	      artificial.setAchievement(new BigDecimal(0));
                        }
                         if (null != list.get(7)) {
                             artificial.setHouse(new BigDecimal(list.get(7)));
                         } else {
                      	      artificial.setHouse(new BigDecimal(0));
                        }
                         if (null != list.get(8)) {
                             artificial.setTraffic(new BigDecimal(list.get(8)));
                         } else {
                      	      artificial.setTraffic(new BigDecimal(0));
                        }
                         if (null != list.get(9)) {
                             artificial.setSkill(new BigDecimal(list.get(9)));
                         } else {
                      	      artificial.setSkill(new BigDecimal(0));
                        }
                         if (null != list.get(10)) {
                             artificial.setCommunication(new BigDecimal(list.get(10)));
                         } else {
                      	      artificial.setCommunication(new BigDecimal(0));
                        }
                         if (null != list.get(11)) {
                             artificial.setOilpatch(new BigDecimal(list.get(11)));
                         } else {
                      	      artificial.setOilpatch(new BigDecimal(0));
                        }
                        
                         if (null != list.get(12)) {
                             artificial2.setAgeaward(new BigDecimal(list.get(12)));
                         } else {
                      	      artificial.setAgeaward(new BigDecimal(0));
                        }
                         if (null != list.get(13)) {
                             artificial.setHightemperature(new BigDecimal(list.get(13)));
                         } else {
                        	 artificial.setHightemperature(new BigDecimal(0));
                         }
                         
                        if (null != list.get(14)) {
                                 artificial.setBenefit(new BigDecimal(list.get(14)));
                         } else {
                          	      artificial.setBenefit(new BigDecimal(0));
                         }
                        if (null != list.get(15)) {
                            artificial.setMeal(new BigDecimal(list.get(15)));
                        } else {
                     	      artificial.setMeal(new BigDecimal(0));
                        } 
                        if (null != list.get(16)) {
                            artificial.setOvertimepay(new BigDecimal(list.get(16)));
                        } else {
                     	      artificial.setOvertimepay(new BigDecimal(0));
                        } 
                        if (null != list.get(17)) {
                            artificial.setPincometax(new BigDecimal(list.get(17)));
                        } else {
                     	      artificial.setPincometax(new BigDecimal(0));
                        } 
                        if (null != list.get(18)) {
                            artificial.setDeduction(new BigDecimal(list.get(18)));
                        } else {
                     	      artificial.setDeduction(new BigDecimal(0));
                        } 
                       
                        if (null != list.get(19)) {
                            artificial.setService(new BigDecimal(list.get(19)));
                        } else {
                     	      artificial.setService(new BigDecimal(0));
                        }
                        if (null != list.get(20)) {
                            artificial.setSscompany(new BigDecimal(list.get(20)));
                        } else {
                     	      artificial.setSscompany(new BigDecimal(0));
                        }
                        if (null != list.get(21)) {
                            artificial.setSume(new BigDecimal(list.get(21)));
                        } else {
                     	      artificial.setSume(new BigDecimal(0));
                        }
                        long affact = artificialService.updateArtificial(artificial);
                        artificial2=null;
                            if (affact == 0) {
                                flag = 0;
                                break;
                            }
 					}
                 }else {
                	 artificial2.setCreater(staf.getId());
                	 artificial2.setCtime(dateFormat.format(now));
                	 artificial2.setRemark(list.get(0));
        			 
                     if (null != list.get(1)) {
                    	 Long id = artificialService.queryWareByName(list.get(1).trim());
                         artificial2.setWarehouseid(id);
                     } else {
                   	  artificial2.setWarehouseid(null);
                     }
                     if(list.get(2)!=null||!list.get(2).equals("")||list.get(3)!=null||!list.get(3).equals("")) {
                    	 Long id=artificialService.queryUserByNameAndNum(uname,jnum);
                   	  artificial2.setUid(id);
                   	artificial2.setJobnum(jnum);
                     }else{
                   	  artificial2.setUid(null);
                   	 
                     }
                     if (null != list.get(4)) {
                    	 Long id = artificialService.queryPostByName(list.get(4).trim());
                         artificial2.setPid(id);
                         
                     } else {
                   	  artificial2.setPid(null);
                     }
                     if (null != list.get(5)) {
                          artificial2.setBasesalary(new BigDecimal(list.get(5)));
                      } else {
                   	  artificial2.setBasesalary(new BigDecimal(0));
                     }
                     if (null != list.get(6)) {
                         artificial2.setAchievement(new BigDecimal(list.get(6)));
                     } else {
                  	      artificial2.setAchievement(new BigDecimal(0));
                    }
                     if (null != list.get(7)) {
                         artificial2.setHouse(new BigDecimal(list.get(7)));
                     } else {
                  	      artificial2.setHouse(new BigDecimal(0));
                    }
                     if (null != list.get(8)) {
                         artificial2.setTraffic(new BigDecimal(list.get(8)));
                     } else {
                  	      artificial2.setTraffic(new BigDecimal(0));
                    }
                     if (null != list.get(9)) {
                         artificial2.setSkill(new BigDecimal(list.get(9)));
                     } else {
                  	      artificial2.setSkill(new BigDecimal(0));
                    }
                     if (null != list.get(10)) {
                         artificial2.setCommunication(new BigDecimal(list.get(10)));
                     } else {
                  	      artificial2.setCommunication(new BigDecimal(0));
                    }
                     if (null != list.get(11)) {
                         artificial2.setOilpatch(new BigDecimal(list.get(11)));
                     } else {
                  	      artificial2.setOilpatch(new BigDecimal(0));
                    }

                     if (null != list.get(12)) {
                         artificial2.setAgeaward(new BigDecimal(list.get(12)));
                     } else {
                  	      artificial2.setAgeaward(new BigDecimal(0));
                    }
                     if (null != list.get(13)) {
                         artificial2.setHightemperature(new BigDecimal(list.get(13)));
                     } else {
                    	 artificial2.setHightemperature(new BigDecimal(0));
                     }
                     
                    if (null != list.get(14)) {
                             artificial2.setBenefit(new BigDecimal(list.get(14)));
                     } else {
                      	      artificial2.setBenefit(new BigDecimal(0));
                     }
                    if (null != list.get(15)) {
                        artificial2.setMeal(new BigDecimal(list.get(15)));
                    } else {
                 	      artificial2.setMeal(new BigDecimal(0));
                    } 
                    if (null != list.get(16)) {
                        artificial2.setOvertimepay(new BigDecimal(list.get(16)));
                    } else {
                 	      artificial2.setOvertimepay(new BigDecimal(0));
                    } 
                    if (null != list.get(17)) {
                        artificial2.setPincometax(new BigDecimal(list.get(17)));
                    } else {
                 	      artificial2.setPincometax(new BigDecimal(0));
                    } 
                    if (null != list.get(18)) {
                        artificial2.setDeduction(new BigDecimal(list.get(18)));
                    } else {
                 	      artificial2.setDeduction(new BigDecimal(0));
                    } 
                   
                    if (null != list.get(19)) {
                        artificial2.setService(new BigDecimal(list.get(19)));
                    } else {
                 	      artificial2.setService(new BigDecimal(0));
                    }
                    if (null != list.get(20)) {
                        artificial2.setSscompany(new BigDecimal(list.get(20)));
                    } else {
                 	      artificial2.setSscompany(new BigDecimal(0));
                    }
                    if (null != list.get(21)) {
                        artificial2.setSume(new BigDecimal(list.get(21)));
                    } else {
                 	      artificial2.setSume(new BigDecimal(0));
                    }
                    long affact = artificialService.addArtificial(artificial2);
                    artificial2=null;
                 if (affact == 0) {
                     flag = 0;
                     break;
                 }
                 }
             }
         }
         try {
             response.getWriter().print(flag);
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
  	@RequestMapping("/findArtificial")
    public void findArtificial(HttpServletResponse response, Page page, String uname, String date){
    	Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		
		if(uname!=null&& !"".equals(uname)) {
			map.put("uname", uname);
			
		}else {
			map.put("uname", "");
			
			}
		if(date!=null&& !"".equals(date)) {
			map.put("date", date);
			
		}else {
			map.put("date", "");
			
			}
		List<Artificial> artificialList = artificialService.findArtificial(map);
        Long count = artificialService.countArtificials(map);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(artificialList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/deleteArtificial")
    public void deleteTransport(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr){
        if(idArr.length > 0) {
            for (int i = 0; i < idArr.length; i++) {
                Long affectRow =artificialService.deleteArtificial(Long.parseLong(idArr[i]));
                if (affectRow > AFFECT_ROW) {
                    try {
                        ResponseUtil.write(response, affectRow);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        ResponseUtil.write(response, AFFECT_ROW);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
  	 
}
